<template>
	<view class="">
		<!-- 顶部导航 -->
		<fa-navbar title="修改密码"></fa-navbar>
		<view class="u-p-30">
			<u-form :model="form" ref="uForm">
				<u-form-item :label-position="labelPosition" label="旧密码：" prop="oldpassword" label-width="180">
					<u-input type="password" :border="border" placeholder="请填旧密码" v-model="form.oldpassword" />
				</u-form-item>
				<u-form-item :label-position="labelPosition" label="新密码：" prop="newpassword" label-width="180">
					<u-input type="password" :border="border" placeholder="请填写新密码" v-model="form.newpassword" />
				</u-form-item>
				<u-form-item :label-position="labelPosition" label="确认新密码：" prop="renewpassword" label-width="180">
					<u-input type="password" :border="border" placeholder="请确认新密码" v-model="form.renewpassword" />
				</u-form-item>
			</u-form>
			<view class="u-p-30 u-m-t-30">
				<u-button hover-class="none" type="primary" :custom-style="{ backgroundColor: theme.bgColor, color: theme.color }" shape="circle" @click="submit">提交</u-button>
			</view>
		</view>
		<!-- 底部导航 -->
		<fa-tabbar></fa-tabbar>
	</view>
</template>

<script>
export default {
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	data() {
		return {
			labelPosition: 'left',
			border: false,
			form: {
				oldpassword: '',
				newpassword: '',
				renewpassword: ''
			},
			rules: {
				oldpassword: [
					{
						required: true,
						message: '请填写旧密码',
						trigger: ['change', 'blur']
					}
				],
				newpassword: [
					{
						required: true,
						message: '请填写新密码',
						trigger: ['change', 'blur']
					}
				],
				renewpassword: [
					{
						required: true,
						message: '请确认新密码',
						trigger: ['change', 'blur']
					},
					{
						validator: (rule, value, callback) => {
							return this.form.newpassword == value;
						},
						message: '两个新密码不一样，请确认',
						// 触发器可以同时用blur和change
						trigger: ['change', 'blur']
					}
				]
			}
		};
	},
	methods: {
		submit() {
			this.$refs.uForm.validate(async valid => {
				if (valid) {
					let res = await this.$api.getUserResetpwd(this.form);
					this.$u.toast(res.msg);	
					if(res.code){
						setTimeout(()=>{
							this.$Router.back(1)
						},800)
					}
					//重置成功的操作
				} else {
					console.log('验证失败');
				}
			});
		}
	}
};
</script>

<style lang="scss"></style>
